package critters;
/* 
 * AP(r) Computer Science GridWorld Case Study:
 * Copyright(c) 2005-2006 Cay S. Horstmann (http://horstmann.com)
 *
 * This code is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * @author Chris Nevison
 * @author Barbara Cloud Wells
 * @author Cay Horstmann
 */

import info.gridworld.actor.Actor;
import info.gridworld.actor.Critter;
import info.gridworld.actor.Rock;
import info.gridworld.grid.Grid;
import info.gridworld.grid.Location;

import java.awt.Color;
import java.util.ArrayList;

/**
 * A <code>CrabCritter</code> looks at a limited set of neighbors when it eats and moves.
 * <br />
 * This class is not tested on the AP CS A and AB exams.
 */
public class KingCrab extends CrabCritter
{
    public KingCrab()
    {
        setColor(Color.RED);
    }

    /*public void processActors(ArrayList<Actor> actors)
    {
    	Grid gr = getGrid();
    	Location nextLoc;
    	for(Actor a : actors){
    		nextLoc = a.getLocation().getAdjacentLocation(getDirection());
    		if(gr.isValid(nextLoc) && getGrid().get(nextLoc) == null)
    			a.moveTo(nextLoc);
    		else{
    			a.removeSelfFromGrid();
    		}
        }
    }*/
    
    public void processActors(ArrayList<Actor> actors)
    {
    	Grid gr = getGrid();
    	int d;
    	Location nextLoc;
    	Location originLoc;
    	for(Actor a : actors){
    		originLoc = a.getLocation();
    		d = getLocation().getDirectionToward(a.getLocation());
    		for( int i=d - Location.HALF_RIGHT; i <= d + Location.HALF_RIGHT; i+=Location.HALF_RIGHT  ){
    			nextLoc = a.getLocation().getAdjacentLocation(i);
        		if(gr.isValid(nextLoc) && getGrid().get(nextLoc) == null){
        			a.moveTo(nextLoc);
        			break;
        		}
    		}
    		if( a.getLocation().equals(originLoc))
    			a.removeSelfFromGrid();
    	}
    }
    
}
